<?php
namespace Api\Service;
/**
 * @todo 支付API
 * @author fzh <fanzhaohai@hrbjjhh.com>
 * @since 2015-11-23
 */
class PayService{
	 
	 protected $error =   '';
	 protected $auth_id = '1';
	 protected $auth_key = 'YUI^&*^&^%^&$%^#$%#$@#$@#';
	 protected $pay_url = 'http://api.pangniao.cn/pay.html';
	 protected $pay_key = '*()*)*&*(&^&UHJUYR%^#$%@#$@#$';
	 
	 
	 /**
	  * 获取微信Openid
	  * @return json
	  * @author fzh<fanzhaohai@hrbjjhh.com>
	  *
	  *  $arr=array(
	  *    "client_order_no" => 'X000011K',
	  *    "url"=>"http://ta.hrbjjhh.com/xstore/cart/?orderid=X000011K"
	  *  );
	  */
	 public function getOpenId($arr=array()){
	     if(!isset($arr['client_order_no']) || empty($arr['client_order_no'])){
	         $this->error = '订单号码错误';
	         return false;
	     }
	     if(!isset($arr['url']) || empty($arr['url'])){
	         $this->error = 'url错误';
	         return false;
	     }
	     import('Common.Msj.Crypt','','.class.php');
	     import('Common.Msj.Curl','','.class.php');
	     $data=array(
	         'auth_id'=>$this->auth_id,
	         'auth_key'=>$this->auth_key,
	         'data'=>$arr,
	         'action'=>'getOpenId'
	     );
	     $data['key'] = self::data_auth_sign($data);
	     $encryt_string = \Crypt::encrypt(json_encode( $data ), $this->weixin_key);
	     $curl = new \Curl();
	     return $curl->curl_post($this->pay_url,array('para'=>$encryt_string));
	 }
	 
	 /**
	  * 支付接口
	  * @param  array  $data 支付数据
	  * @return json
	  * @author fzh<fanzhaohai@hrbjjhh.com>
	  *
	  * @example
	  * array(
	  *    'goods_title'=>'商品名称',
	  *    'goods_des'=>'描述',
	  *    'order_money'=>'付款金额',
	  *    'pay_type'=>'支付方式',
	  *    'set_url'=>'支付成功后回调地址;需要是一个绝对路径的Url,我这面会做验证',
	  *    'call_back_url'=>'post回去异步通知,会做这么几个事,一个是post请求你可以接收moeny,param两个参数,param参数回调的时候会记录订单号',
	  *)
	  */
	 public function pay($order_array){
	     if(!is_array($order_array)||empty($order_array)){
	         $this->error = '参数错误';
	         return false;
	     }
	 
	     if(!isset($order_array['goods_title']) || empty($order_array['goods_title'])){
	         $this->error = '商品名称不能为空';
	         return false;
	     }
	     if(!isset($order_array['order_money']) || (float)$order_array['order_money'] <= 0){
	         $this->error = '付款金额错误';
	         return false;
	     }
	     //这里待完善
	     if(!isset($order_array['pay_type']) ||  !in_array($order_array['pay_type'],array('alipay','wechatpay','wechatwappay'))){
	         $this->error = '支付方式错误';
	         return false;
	     }
	     if(!isset($order_array['set_url']) || empty($order_array['set_url'])){
	         $this->error = '同步回掉地址错误';
	         return false;
	     }
	     if(!isset($order_array['call_back_url']) || empty($order_array['call_back_url'])){
	         $this->error = '异步回掉地址错误';
	         return false;
	     }
	 
	     $order_array = array(
	         'auth_id'=>$this->auth_id,
	         'auth_key'=>$this->auth_key,
	         "data"=> $order_array,
	         'action'=>'pay'
	     );
	     $order_array['key'] = self::data_auth_sign($order_array);
	     $encryt_string = $this->callBackByAes($order_array);
	     import('Common.Msj.Curl','','.class.php');
	     $curl = new \Curl();
	     $result = $curl->curl_post($this->pay_url,array('para'=>$encryt_string));
	     return json_decode($result,true);
	 }
	 
	 
	 /**
	  * 查询订单支付状态
	  * @param  array  $data
	  * @return json
	  * @author fzh<fanzhaohai@hrbjjhh.com>
	  *
	  * @example
	  * array(
	  *    'out_trade_no'=>'支付网关订单号',
	  *    'pay_type'=>'支付方式',
	  *)
	  *
	  */
	 //最终支付网关统一之后再做优化
	 public function getOrderStatus($order_array){
	     if(!is_array($order_array)||empty($order_array)){
	         $this->error = '参数错误';
	         return false;
	     }
	     
	     if(!isset($order_array['out_trade_no']) || strlen($order_array['out_trade_no'])!=15){
	         $this->error = '支付订单号错误';
	         return false;
	     }
	 
	     if(!isset($order_array['pay_type']) ||  !in_array($order_array['pay_type'],array('alipay','wechatpay','wechatwappay'))){
	         $this->error = '支付方式错误';
	         return false;
	     }
	     
	     $order_array = array(
	         'auth_id'=>$this->auth_id,
	         'auth_key'=>$this->auth_key,
	         'data'=> $order_array,
	         'action'=>'getOrderQuery'
	     );
	     $order_array['key'] = self::data_auth_sign($order_array);
	     $obj =  new \Think\Crypt\Driver\Think();
	     $encryt_string = $obj->encrypt(urldecode(json_encode( $order_array )), $this->pay_key);
	     import('Common.Msj.Curl','','.class.php');
	     $curl = new \Curl();
	     $result = $curl->curl_post($this->pay_url,array('para'=>$encryt_string));
	     return json_decode($result,true);
	 }
	 
	 
	 /**
	  * @todo  生成经过Aes加密后的字符串
	  * @param  array $json_data
	  * @return string
	  * @Author: PHP@妖孽 <dino_ma@163.com>
	  */
	 private function callBackByAes(&$return_data){
// 	     $obj =  new \Think\Crypt\Driver\Think();
	     $obj =  new \Think\Crypt();
	     return $obj->encrypt(urldecode(json_encode( $return_data )), $this->pay_key);
	 }
	 
	 /**
	  * 数据签名认证
	  * @param  array  $data 被认证的数据
	  * @return string       签名
	  * @author PHP@妖孽  <dino_ma@163.com>
	  */
	 private static function data_auth_sign($data) {
	     //数据类型检测
	     if(!is_array($data)){
	         $data = (array)$data;
	     }
	     ksort($data); //排序
	     $code = http_build_query($data); //url编码并生成query字符串
	     $sign = sha1($code); //生成签名
	     return $sign;
	 }
	 
	
	/**
	 * @todo 返回最后一次错误记录
	 * @return string
	 */
	public function getError(){
		return $this->error;
	}
}
